/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fatclientnbp;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import edu.chl.chalmers.fatclient.sb.Flight;
import AirLine.*;
import java.awt.*;

/**
 *
 * @author smulle
 */
public class ConfirmationPanel extends JPanel implements ActionListener{
    private ArrayList<Booking> bookings;
    private ArrayList<JPanel> panelList;
    private JButton backToSearch;
    private MainController controller;

    public ConfirmationPanel(ArrayList<Booking> bookings, MainController controller) {
        this.bookings= bookings;
        this.controller = controller;
        panelList=new ArrayList<JPanel>();
        createTables();
        showTables();
        System.out.println("nu skall det vara en ny panel");
    }
    
        private void createTables()
        {
            String[] columns={"Passenger", "SSN", "Airline", "Departure Airport", "Destination Airport", "Departure Time", "Destination Time", "Price"};
            int j=0;
		for(Booking b: bookings)
                {
                    
                   // DefaultTableModel model=new DefaultTableModel(7, t.getFlights().size()+1);       
                    JTable table=new JTable(bookings.size()+1, 8);
                    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
                    
                    for(int k=0; k<columns.length; k++)
                    {
                        table.setValueAt(columns[k], 0, k);
                    }
                    table.getTableHeader().setBackground(Color.yellow);
                    
                    table.repaint();
                     table.getTableHeader().repaint();
                     table.getTableHeader().setBackground(Color.blue);
                    
                    JPanel panel=new JPanel();
                    panel.setLayout(new GridBagLayout());
                    GridBagConstraints c = new GridBagConstraints();
                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx=0;
                    c.gridy =0;
                    int i=0;
                    Flight f=b.getFlight();
                    Seat s=b.getSeat();
                    i++;
                        table.setValueAt(b.getPerson().getName(), i, 0);
                        table.setValueAt(b.getPerson().getSSN(), i, 1);
                        table.setValueAt(f.getAirline().getName(), i, 2);
                        table.setValueAt(f.getDepartureAirport(), i, 3);
                        table.setValueAt(f.getDestinationAirport(), i, 4);
                        table.setValueAt(f.getDepartureTime(), i, 5);
                        table.setValueAt(f.getArrivalTime(), i, 6);
                        table.setValueAt(f.getPrice(), i, 7);
                        table.setEnabled(false);
                    
                   panel.add(table,c);
                   //c.gridx++;
                  // c.gridy++;
                   panelList.add(panel);

                   j++;
                }	    
        }
        
        private void showTables()
        {
            this.setLayout(new GridBagLayout());
             GridBagConstraints c = new GridBagConstraints();
                c.fill = GridBagConstraints.HORIZONTAL;
                c.gridx=0;
                c.gridy=0;
            this.add(new JLabel("Bookings completed, info below: "));
            
            c.gridy++;
                
            for(JPanel p: panelList)
            {
                this.add(p, c);
                c.gridy++;
                
            }
            c.gridx++;
            c.gridy++;
            backToSearch = new JButton("Continue");
            backToSearch.addActionListener(this);
            this.add(backToSearch,c);
        }
        
    	public void actionPerformed(ActionEvent e) 
	{
            controller.start();
        }
    
}
